package com.han.springboot01.controller;

import com.han.springboot01.mapper.IManager;
import com.han.springboot01.pojo.Manager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;

@Slf4j
@RequestMapping("user")
@RestController
public class LoginController {

    @Autowired
    private IManager iManager;

    @GetMapping("loginname")
    public ModelAndView login(@RequestParam("username") String name, @RequestParam("password") String password){
        ModelAndView success = new ModelAndView("redirect:/success.html");
        ModelAndView dashboard = new ModelAndView("redirect:/dashboard.html");
        if (StringUtils.isEmpty(name)){
            System.out.println("-----34------");
            return dashboard;
        }
        if (StringUtils.isEmpty(password)){
            return dashboard;
        }
        Manager manager=iManager.selectManager(name);
        if (manager==null){
            return dashboard;
        }
//        if (Objects.equals(password, manager.getMpassword())){
//            System.out.println("----2323-----");
//            return "登录成功";
//
//        }else
        if (Objects.equals(name, manager.getMname()) & Objects.equals(password, manager.getMpassword())){
            return success;
        }
        return dashboard;
    }




//    public String login(@RequestParam("username") String name, @RequestParam("password") String password){
//        if (StringUtils.isEmpty(name)){
//            System.out.println("-----34------");
//            return "用户名不许为空";
//        }
//        if (StringUtils.isEmpty(password)){
//            return "密码不许为空";
//        }
//        Manager manager=iManager.selectManager(name);
//        if (manager==null){
//            return "无此用户名";
//        }
////        if (Objects.equals(password, manager.getMpassword())){
////            System.out.println("----2323-----");
////            return "登录成功";
////
////        }else
//        if (Objects.equals(name, manager.getMname()) & Objects.equals(password, manager.getMpassword())){
//            return new ModelAndView"redirect:/success";
//        }
//        return "登陆用户名或密码错误";
//    }
//





    @GetMapping("register")
    public String register(@RequestParam("username") String name, @RequestParam("password") String password){
        log.info("name:{}",name);
        log.info("password:{}",password);
if (StringUtils.isEmpty(name)){
    System.out.println("-----34------");
    return "用户名不许为空";
}
 if (StringUtils.isEmpty(password)){
    return "密码不许为空";
}
        Manager manager=iManager.selectManager(name);
        if (manager!=null){
            return "用户存在";
        }
        int resultCount=iManager.saveUser(name,password);
        if(resultCount==0){
            return "注册失败";
        }

        return  "1111注册成功";
    }

//    @GetMapping("login")
//    public String login(){
//        return "登陆成功";
//    }
}
